<template>
  <div class="card-container">
    <el-form :model="form" :rules="formRules" ref="formRef" label-width="120px" style="width: 100%;" label-position="right">
      <el-card class="box-card" shadow="never">
        <div slot="header" class="clearfix">
          <span>{{ form.pid == 0 ? '一' : '二' }}级路由</span>
        </div>
        <el-form-item label="后台标题：" prop="backTitle">
          <el-input v-model="form.title"></el-input>
          <div class="tips">后台显示的标题</div>
        </el-form-item>
        <el-form-item label="排序：" prop="">
          <el-input v-model="form.sort"></el-input>
        </el-form-item>
        <el-form-item label="路由标题：" prop="title">
          <el-input v-model="form.content.meta.title" placeholder="如：商品管理"></el-input>
          <div class="tips">设置该路由在侧边栏和面包屑中展示的名字</div>
        </el-form-item>
        <el-form-item label="路由名称：" prop="name">
          <el-input v-model="form.content.name" placeholder="如：goodsList"></el-input>
          <div class="tips">设置路由的名字，一定要填写并且要跟view的name要对应</div>
        </el-form-item>
        <el-form-item label="路由路径：" prop="path">
          <el-input v-model="form.content.path" placeholder="如：/goodsList"></el-input>
          <div class="tips">设置路由的路径</div>
        </el-form-item>
        <el-form-item label="组件路径：" prop="path">
          <el-input v-model="form.content.component" placeholder="如：goods/list/index"></el-input>
          <div class="tips">设置组件的路径，一级路由填写Layout</div>
        </el-form-item>
        <el-form-item label="图标：" prop="icon">
          <el-input v-model="form.content.meta.icon" placeholder="如：shopping-bag"></el-input>
          <div class="tips">设置该路由的图标，在常规图标中拷贝即可</div>
        </el-form-item>
        <el-form-item label="重定向路径：" prop="redirect" v-if="form.pid == 0">
          <el-input v-model="form.content.redirect"></el-input>
          <div class="tips">设置该路由重定向的路径，设为noRedirect的时候，该路由在面包屑导航中不可被点击</div>
        </el-form-item>
        <el-form-item label="是否缓存：">
          <div class="radio-box">
            <el-radio v-model="form.content.meta.noKeepAlive" label="false">是</el-radio>
            <el-radio v-model="form.content.meta.noKeepAlive" label="true">否</el-radio>
          </div>
          <div class="tips">设置该路由是否启用缓存</div>
        </el-form-item>
        <el-form-item label="是否隐藏：">
          <div class="radio-box">
            <el-radio v-model="form.content.hidden" label="true">是</el-radio>
            <el-radio v-model="form.content.hidden" label="false">否</el-radio>
          </div>
          <div class="tips">设置该路由是否在左侧菜单中隐藏</div>
        </el-form-item>
        <el-form-item label="显示根节点：" v-if="form.pid == 0">
          <div class="radio-box">
            <el-radio v-model="form.content.alwaysShow" label="true">是</el-radio>
            <el-radio v-model="form.content.alwaysShow" label="false">否</el-radio>
          </div>
          <div class="tips">是否显示根节点，如果children只有一个时此项为false，会不显示父级</div>
        </el-form-item>
      </el-card>
      <el-form-item>
        <el-button type="primary" @click="submitForm">保存修改</el-button>
      </el-form-item>
    </el-form>

    <!-- 选择商品弹窗 -->
  </div>
</template>

<script>
import { editRoute, getRouteDetail } from '@/api/permission'

export default {
  name: 'editRoute',
  data() {
    return {
      form: {
        title: '',
        pid: 0,
        sort: '1',
        content: {
          name: '',
          path: '',
          component: '',
          redirect: '',
          alwaysShow: true,
          meta: {
            title: '',
            icon: '',
            noKeepAlive: true,
          },
        },
      },
      formRules: {},
      isRedirect: false,
    }
  },
  created() {
    // this.form.pid = this.$route.query.pid
    // if (this.form.pid == 0) {
    //   this.form.content.component = 'Layout'
    // }
    this.getRouteDetail()
  },
  methods: {
    async getRouteDetail() {
      const res = await getRouteDetail(this.$route.query.id)
      if (res.status != 1) return this.$message.error(res.msg)
      this.$message.success(res.msg)
      console.log(res)
      // res.data.content.component = 'views/' + res.data.content.component
      this.form = res.data
    },
    async submitForm() {
      // this.$refs.formRef.validate(async valid => {
      //   if (!valid) return false
      // })
      if (this.form.pid != 0) {
        this.$delete(this.form, 'redirect')
        this.$delete(this.form, 'alwaysShow')
      }
      const res = await editRoute(this.form)
      if (res.status != 1) return this.$message.error(res.msg)
      this.$message.success(res.msg)
      this.closePage()
    },
    resetForm() {
      this.$refs.formRef.resetFields()
      this.form = this.$options.data().form
    },
    async closePage() {
      let view = this.$route
      await this.$store.dispatch('tagsBar/delRoute', view)
      this.$router.push('/routeList')
    },
  },
}
</script>

<style lang="scss" scoped>
// .el-form-item {
//   width: 420px;
// }
::v-deep .el-input__inner {
  // margin-left: 20px;
  height: 36px;
  width: 420px;
}
.tips {
  color: #999;
  margin-top: 10px;
  font-size: 12px;
}
.el-card {
  border-radius: 8px;
}
::v-deep .el-card__header {
  font-size: 14px;
  color: #555;
  font-weight: bold;
}
::v-deep .el-form-item__label {
  padding-right: 12px;
}
::v-deep .el-range-separator {
  line-height: 28px;
}
::v-deep .el-range-input {
  cursor: pointer;
}
.radio-box {
  display: flex;
  align-items: center;
  height: 36px;
  width: 600px;
}
</style>
